package org.spring.springboot.app.community.housecost.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.community.housecost.domain.vo.CommunityHouseCostVO;
import org.spring.springboot.app.community.housecost.domain.vo.HouseCostSearchVO;
import org.spring.springboot.app.community.housecost.domain.vo.TaskHouseCostVO;
import org.spring.springboot.app.community.housecost.domain.vo.UpdateHouseCostVO;
import org.spring.springboot.app.community.housecost.service.ICommunityHouseCostService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description="社区-房间物业费用管理接口",tags = ApiIndex.CommunityHouseCostController)
@RequestMapping(value = "/api/communityHouseCost")
@RestController
public class CommunityHouseCostController {
    @Autowired
    private ICommunityHouseCostService communityHouseCostService;

    @ApiOperation(value="查询所有房间物业费用列表")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<CommunityHouseCostVO>>> findAll(
            @ApiParam(value = "房间名", required = false) @RequestParam(defaultValue = "") String houseName,
            @ApiParam(value = "业主姓名", required = false) @RequestParam(defaultValue = "") String userName,
            @ApiParam(value = "业主电话", required = false) @RequestParam(defaultValue = "") String userMobile,
            @ApiParam(value = "是否缴纳", required = false) @RequestParam(defaultValue = "") String hasGive,
            @ApiParam(value = "费用类型", required = false) @RequestParam(defaultValue = "") String type,
            @ApiParam(value = "缴费途径", required = false) @RequestParam(defaultValue = "") String channel,
            @ApiParam(value = "机构ID（如果传0 从后台的token里取）", required = false) @RequestParam(defaultValue = "0") String office_id,
            @ApiParam(value = "开始所属日期", required = false) @RequestParam(defaultValue = "") String startYearMouth,
            @ApiParam(value = "结束所属日期", required = false ) @RequestParam(defaultValue = "") String endYearMouth,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ){

        HouseCostSearchVO searchVo=new HouseCostSearchVO();
        searchVo.setHouseName(houseName);
        searchVo.setUserName(userName);
        searchVo.setUserMobile(userMobile);
        searchVo.setHasGive(hasGive);
        searchVo.setType(type);
        searchVo.setStartYearMouth(startYearMouth);
        searchVo.setEndYearMouth(endYearMouth);
        searchVo.setChannel(channel);
        searchVo.setOfficeId(office_id);
        R<PageInfo<List<CommunityHouseCostVO>>> result = new R();
        List<CommunityHouseCostVO> list = communityHouseCostService.findAll(searchVo,pageNum,pageSize);
        PageInfo p = new PageInfo(list);

        result.setData(p);
        return result;
    }

    /**
     * 删除
     * @param id
     */
    @ApiOperation(value="删除物业费信息")
     @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
     @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(@ApiParam(value = "物业费Id", required = true) @PathVariable("id") String id) {
        R result = new R();
        int code = communityHouseCostService.delete(id);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("刪除失败");
        }
        return result;
    }



    /**
     * 修改
     */
    @ApiOperation(value="修改物管费用")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(@ApiParam(value = "房间物管费用表", required = true) @RequestBody UpdateHouseCostVO vo) {
        if(StringUtils.isBlank(vo.getChannel())){
            vo.setChannel("1");
        }
        R result =  communityHouseCostService.update(vo);
        return result;
    }


    /**
     * 生成物管費
     */
    @ApiOperation(value="生成物管費")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(@ApiParam(value = "需要生成的房間物管費的參數", required = true)@RequestBody TaskHouseCostVO vo) {
        R result =  communityHouseCostService.createHouserCost(vo);
        return result;
    }



    /**
     *  更新房间到期时间
     * @param id
     */
    @ApiOperation(value="更新房间到期时间")
    @RequestMapping(value = "/replaceHouseExpireDay", method = RequestMethod.POST)
    public R replace(@ApiParam(value = "房间Id", required = true) @RequestParam(defaultValue = "")String houseId) {
        R result = new R();
        if(StringUtils.isBlank(houseId)){
            return  result.fail().setMessage("房间ID不能为null");
        }
        return communityHouseCostService.replaceHouseExpireDay(houseId);
    }

}
